<template>
    <div class="bindingShops">
      <div v-if="userInfo.admin !='微笑' && userInfo.user !='微笑'">
        <div class="title">
          <!--<img style="margin-left:10px;width:7px;height:10px;margin-bottom:2px;" src="../../assets/duos.png" alt="">-->
          <!--<h3 style="margin-left:-10px;color:black;">绑定新店铺</h3>-->
        </div>
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <!-- <el-form-item :rules="[
                      { required: true, message: '店铺首页链接不能为空', trigger: 'blur' },
                    ]" label="店铺首页链接" prop="index" style="position: relative;left: 15px">
                            <el-input v-model="form.index">
                            </el-input>
                        </el-form-item>
    -->
          <el-form-item label="店铺名" prop="wangwang" style="position: relative;left: 15px">
            <el-input v-model="form.shop" placeholder="请输入店铺名" style="width: 215px;margin-top:-9px;">
            </el-input>
          </el-form-item>
          <!-- <el-form-item label="多多号名" prop="wangwang" style="position: relative;left: 48px">
              <el-input v-model="form.taobao" placeholder="请输入多多号名" style="height:30px;width: 215px;margin-top:-9px;">
              </el-input>
          </el-form-item> -->
          <!--  <el-form-item label="验证码" style="position: relative;left: 64px">
                            <el-input placeholder="请输入内容" v-model="input4">
                                <template slot="append">
                                    复制
                                </template>
                            </el-input>
                        </el-form-item>-->

          <el-form-item style="position: relative;left: 10px;">
            <el-button :loading="bindloading" @click="onSubmit" type="primary" style="width:100px;" size="mini">
              绑定
            </el-button>
          </el-form-item>
<!--          <el-form-item style="position: relative;left: 5px;">-->
<!--            <el-button size="mini" type="danger" icon="el-icon-star-off" @click="classDialog = true">教程</el-button>-->
<!--          </el-form-item>-->
        </el-form>
      </div>
      <div class="titles">
        <h3 style="float:left;margin:0;">已绑定的店铺</h3>
      </div>
      <!-- <el-date-picker v-model="dateValue" type="date" placeholder="选择日期" style="margin-bottom:20px;margin-left:11px;" @change="changeTime"
                      value-format="yyyy-MM-dd" :default-value="currentTime">
      </el-date-picker> -->
      <el-table v-loading="tableloading" :data="tableData4" max-height="460" style="width: 100%" border >
        <el-table-column type="index" align="center" prop="company_id" label="序号" width="60">
        </el-table-column>
        <el-table-column label="店铺帐号" prop="shopname" align="center" width="">
        </el-table-column>
<!--        <el-table-column label="授权" prop="status" align="center" width="">-->
<!--          <template slot-scope="scope">-->
<!--            <span style="color:rgb(129,215,64);text-decoration:underline;cursor: pointer;" type="primary" @click="active(scope)">授权</span>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column label="绑定时间" prop="create_time" align="center" width="">
        </el-table-column>
<!--        <el-table-column label="激活" prop="update_time" align="center" width="">-->
<!--        </el-table-column>-->
        <el-table-column label="店铺操作" prop="taobao" width="200" align="center">
          <template slot-scope="scope">
            <el-button size="mini"  type="primary" @click="dlshop(scope)">删除</el-button>
<!--            <el-button size="mini"  type="danger" @click="activation(scope.row)" v-if="scope.row.status == 0">待激活</el-button>-->
<!--            <el-button size="mini"  type="info" v-if="scope.row.status == 2">已激活</el-button>-->
          </template>
        </el-table-column>
      </el-table>
    </div>
</template>
<script>
  import { userinfo,addshop,shoplist,delectshop,synchronizeshop } from '../../api/api'
  export default {
    data() {
      return {
        // dateValue: "",
        defaultDate: "",
        currentTime: "",
        form: {
          shop: "",
          taobao: ""
        },
        radio: "1",
        formInline: {
          user: "",
          region: ""
        },
        userInfo: {},
        tableData4: [],
        money: "",
        isShow: false, //是否显示
        bindloading:false,
        tableloading:false,
        classDialog:false,
      };
    },

    //初始化
    created(){
      this.getShopList()
      this.getUser()
    },
    methods: {
      //获取用户信息
      getUser(){
        userinfo().then(res=>{
          if(res.Status == 200){
            this.$emit('getMoney',res.Data.money)
          }else{
            this.$message.error(res.Message)
          }
        })
      },
      // getRowClass ({ row, column, rowIndex, columnIndex }) {
      //   if (rowIndex === 0) {
      //     return 'background:rgba(237,246,255,1);color:#7B7B7B;text-align:center;font-size:9px;font-weight:500;'
      //   } else {
      //     return ''
      //   }
      // },
      // changeTime() {
      //   this.currentTime = this.dateValue;
      // },
      // 当前时间
      // nowTime() {
      //   let time = new Date();
      //   let year = time.getFullYear();
      //   let month = time.getMonth() + 1;
      //   let date = time.getDate();
      //   month < 10 ? (month = "0" + month) : month;
      //   date < 10 ? (date = "0" + date) : date;
      //   this.currentTime = year + "-" + month + "-" + date;
      //   this.dateValue = this.currentTime;
      // },
      // 下载店铺数据
      // downLoad(item) {
      //   window.location.href =
      //       "/api/xlsx.php?shop=" +
      //       encodeURIComponent(item.row.shop) +
      //       "&time=" +
      //       this.currentTime;
      // },
      //删除
      dlshop(item){
        var DlshopParams = {
          id:item.row.id,
        }
        delectshop(DlshopParams).then(res=>{
          if(res.Status == 200){
            this.getShopList()
            this.$message({
              type:"success",
              message:res.Message
            })
          }else{
            this.$message.error(res.Message)
          }
        })
      },
      //shop列表
      getShopList() {
        this.tableloading = true
        shoplist().then(res=>{
          this.tableloading = false
          if(res.Status == 200){
            for(let item of res.Data) {
              var timestamp4 = new Date(item.create_time* 1000)
              let timer = new Date(item.update_time* 1000)
              item.create_time =  timestamp4.toLocaleDateString().replace(/\//g, "-") + " " + timestamp4.toTimeString().substr(0, 8)
              item.update_time = timer.toLocaleDateString().replace(/\//g, "-") + " " +timer.toTimeString().substr(0, 8)
            }
            this.tableData4 = res.Data
          }
        })
      },
      //添加店铺
      onSubmit() {
        let _this = this;
        _this.bindloading = true
        if (_this.form.shop === "") {
          _this.$message({
            type: "error",
            message: "店铺名不能为空"
          });
          return;
        }else{
          var AddshopParams = {
            shopname:_this.form.shop,
          }
          addshop(AddshopParams).then(res=>{
            _this.bindloading = false
            if(res.Status == 200){
              _this.form.shop = ''
              _this.getShopList()
              _this.$message({
                type:"绑定成功",
                message:res.Message
              })
            }else{
              _this.$message.error(res.Message)
            }
          })
        }
      },
      //授权跳转
      active(item) {
        let _this = this;
        window.location = "https://oauth.taobao.com/authorize?response_type=code&client_id=23700513&redirect_uri=http://trade.taoesou.com/default.aspx&view=web&state=http://api.taoesou.com/api.aspx?platform=502%26act=bindshop_do%26shop=" + btoa(encodeURI("{\"shop\":\"" + item.row.shopname + "\",\"sid\":8}"));
        // window.location ="https://oauth.taobao.com/authorize?response_type=code&client_id=23700513&redirect_uri=http://trade.taoesou.com/default.aspx&view=web&state=http://api.taoesou.com/api.aspx?platform=285%26act=bindshop_do%26shop=" + btoa(encodeURI("{\"shop\":\"" + item.row.shopname + "\",\"sid\":2,\"belong\":2}"))
      },
      //激活同步
      activation(row){
        let params = {
          shopname:row.shopname
        }
        synchronizeshop(params).then(res=>{
          if(res.Status == 200){
            this.$message.success(res.Message)
            this.getShopList()
          }else{
            this.$message.error(res.Message)
          }
        })
      },
    },
    mounted() {
      // this.nowTime();
    }
  };
</script>
<style lang="scss">
  .bindingShops {
    .titles{
      overflow: hidden;
      padding:15px 10px;
    }
    .el-input__inner{
      height:30px;
    }
    .el-input__icon{
      margin-top:-5px;
    }
    .title h3 {
      display: inline-block;
      padding-left: 20px;
    }

  }
</style>
